<?php
$libdir = dirname(__FILE__);
require_once $libdir.'/coreLib.php';
class CssRule extends lib {
	protected $rule = '';
	protected $atributes = array();
	const ONE_LINE = false;
	const MULTI_LINE = true;
	/**
	 * Creates a CSS rule
	 * @param string $rule
	 * @param array() $atributes
	 */
	public function __construct($rule = null,$atributes = null) {
		if(!is_null($rule)) $this->rule = $rule;
		if(!is_null($atributes) && is_array($atributes)) {
			foreach($atributes as $name=>$value){
				if($this->checkCssAtribute($name)){
					$this->addAtribute($name, $value);
				}//if checkCssAtribute
			}//foreach
		}//if(!is_null($rule...
	}
	/**
	 * Chack a string if it is a valid CSS atribute
	 * @param string $name
	 */
	public function checkCssAtribute($name){
		if(preg_match("/[^a-z0-9\-]/i", $name) != 0) return FALSE;
		return true;
	}
	/**
	 * Add a atribute to the CSS rule
	 * @param string $name
	 * @param string $value
	 */
	public function addAtribute($name, $value){
		if(!$this->checkCssAtribute($name)) return;
		$this->atributes[$name] = $value;
	}
	/**
	 * Get a specific atribute value
	 * @param string $name
	 */
	public function getAtribute($name){
		if(isset($this->atributes[$name])) return $this->atributes[$name];
	}
	/**
	 * load the CSS rule into a string
	 * @param unknown_type $newline
	 */
	public function load($newline = true){
		$css = "";
		$css.= $this->rule.'{';
		if($newline) $css.= "\n";
		$css.=$this->loadAtributes($newline);
		$css.= "}\n";
		return $css;
	}
	public function loadAtributes($newline){
		$css= '';
		foreach($this->atributes as $name=>$value){
			$css.="\t".$name.":".$value.";";
			if($newline) $css.= "\n";
		}
		return $css;
	}
}